#!/usr/bin/python3
########################################################################
# 生成滤波器序列数据为c文件
# Copyright (C) 2022  Xu Ruijun
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
########################################################################
import numpy as np
import datetime
from np2c import numpy2ccode

#为了节省空间，利用对称性，只保存一侧数据
def gen_halfhann(n):
    t = np.arange(np.pi/n/2, np.pi, np.pi/n)
    y = (1-np.cos(t))/2
    y32 = np.round((y*2**32)).astype(np.uint32)
    return y32


if __name__ == '__main__':
    dtstr = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    with open('../User/filter_data.c', 'w') as f:
        f.write(f'//generated by {__file__} {dtstr}\n')
        np2c = numpy2ccode(f, '{:10d}', Ncol=8)
        for i in range(2, 8):
            n2 = 2**i
            y = gen_halfhann(n2)
            np2c.write_1d(f'win_hann{n2*2}', y)
